﻿using ConfExpert.Forms.Base;
using ConfExpert.Utility;
using ConfExpert.Utility.Peripheral;
using DevTeam;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo
{
    public partial class TopTarama : XForm
    {
        public TopTarama()
        {
            InitializeComponent();
        }

        private void StokKartiDestex_Load(object sender, EventArgs e)
        {
            EnableBarcodeType(0, 10);
            BarcodeRead += PO_BarcodeRead;
            
            //((RepositoryItemTextEdit)textBox1.Edit).KeyPress += textBox1_KeyPress;
            //this.Shown += TopEtiketleme_Shown;

            btnKaydet.ItemClick += btnKaydet_ItemClick;
            btnKaydet.Enabled = false;

        }
        
        int ShelfId = 0, RollId = 0;

        void PO_BarcodeRead(int Type, int Barcode)
        {
            if (Type > 0)
            {
                ShelfId = Barcode;
                this.Invoke(new MethodInvoker(() => txtInfo.Caption = AppSession.Lookups.Shelf.FindByShelfId(Barcode).Name));
            }
            else
            {
                RollId = Barcode;
                Scan(RollId);
                if (photosRTable.Table.Rows.Count > 0)
                {
                    btnKaydet.Caption = photosRTable.Table.Rows.Count.ToString() + " Kaydet";
                    btnKaydet.Enabled = true;
                }
                else
                {
                    btnKaydet.Caption = "Kaydet";
                    btnKaydet.Enabled = false;
                }
            }
        }

        /*
        void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                barkod = textBox1.EditValue.ToStr().Substring(0, textBox1.EditValue.ToStr().Length - 1).ToInt();
                if (barkod > 1e9)
                {
                    ShelfId = barkod - 1000000000;
                    txtInfo.Caption = AppSession.Lookups.Shelf.FindByShelfId(ShelfId).Name;
                }
                else
                {
                    RollId = barkod;
                    Scan(RollId);
                    if(photosRTable.Table.Rows.Count>0)
                    {
                        btnKaydet.Caption = photosRTable.Table.Rows.Count.ToString() + " Kaydet";
                        btnKaydet.Enabled = true;
                    }
                    else
                    {
                        btnKaydet.Caption = "Kaydet";
                        btnKaydet.Enabled = false;
                    }

                }
                textBox1.EditValue = null;
            }
        }

        int ShelfId = 0, RollId = 0, barkod = 0;

        void TopEtiketleme_Shown(object sender, EventArgs e)
        {
            textBox1.Links[0].Focus();
            ((BarEditItemLink)textBox1.Links[0]).ShowEditor();
        }
        */
        Scanner.PhotosRTable photosRTable = new Scanner.PhotosRTable();

        void Scan(int RollId, bool big = false)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                Image scanned = Scanner.Scan(big);    //kumaş için sabitlendi

                if (scanned != null)
                    photosRTable.AddRow(RollId, scanned, big);      //DİKKAT! ProductId yerine RollId gönderiliyor!
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }

        }

        int SaveCount = 0;
        void btnKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            btnKaydet.Enabled = false;
            Save();

            //TODO: normal taramanın üstüne BIG tarayınca big flag'ini setlemiyor!
            ExecuteNonQuery("UpdPhoto6Roll", photosRTable.Table, Environment.MachineName);

            if ((++SaveCount) % 5 == 0)
                GC.Collect();

            photosRTable = new Scanner.PhotosRTable();

            btnKaydet.Enabled = true;
            Notify("Kaydedildi");
        }

        //void UpdPhoto6(params object[] p)
        //{
        //    using (SqlConnection sqlConn = new SqlConnection(Session.DataManager.ConnectionString))
        //    {
        //        sqlConn.Open();
        //        SqlCommand sqlCmd = sqlConn.CreateCommand();
        //        sqlCmd.CommandText = "UpdPhoto6Roll";
        //        sqlCmd.CommandType = CommandType.StoredProcedure;
        //        SqlParameter[] parameters = new SqlParameter[2];
        //        parameters[0] = new SqlParameter("@tbl", SqlDbType.Structured);
        //        parameters[0].Value = p[0];
        //        parameters[1] = new SqlParameter("@DeviceID", SqlDbType.VarChar);
        //        parameters[1].Value = p[1];
        //        sqlCmd.Parameters.AddRange(parameters);
        //        sqlCmd.ExecuteNonQuery();
        //    }
        //}
    }
}
